using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace DynamicDNS.Provider.TCP
{
    /// <summary>
    /// Implementation for the TCP-based DtDNS Dynamic DNS Provider
    /// </summary>
    public class DtDNSProvider : DynamicDNS.Provider.BaseProvider.DynamicDNSTCPBaseProvider, IDynamicDNSProvider
    {
        /// <summary>
        /// This template contains the basic provider specific request.
        /// </summary>
        private const string REQUESTTEMPLATE = "GET /api/autodns.cfm?" +
            "id={0}" +
            "&pw={1}" +
            "&ip={2}" +
            " HTTP/1.1\r\n" +
            "Host: www.dtdns.com\r\n" +
            "Connection: Close" +
            "User-Agent: DynamicDNS Library\r\n\r\n";

        public DtDNSProvider()
        {
            ProviderDomain = "www.dtdns.com";

        }

        #region IDynamicDNSProvider Member

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourdomainname, string username, string password)
        {
            BuildRequestString(ip, yourdomainname, password);
            return base.UpdateDNSEntry();
             
        }

        /// <summary>
        /// This is a helper method for replacing the template values against the correct values
        /// </summary>
        /// <param name="ip">Contains the IP Adress for which the Update is to processed</param>
        /// <param name="yourdomainname">Contains the Hostname for which the Update is to processed. This is Provider specific.</param>
        /// <param name="password">Contains the required password for the update</param>
        private void BuildRequestString(IPAddress ip, string yourdomainname, string password)
        {
            Request = String.Format(REQUESTTEMPLATE, yourdomainname, ip.ToString(), password);
        }

        /// <summary>
        /// Overrides the basic CheckResult for provider specific result checking of the response
        /// </summary>
        /// <param name="response">The Response, which has been sent by the webserver</param>
        /// <returns>DynamicDNSUpdateResult indicates, whether an Update was successful or not.</returns>
        protected override DynamicDNSUpdateResult CheckResult(string response)
        {
            //Forward to the response and skip the HTTP Header
            response = response.Substring(response.IndexOf("\r\n\r\n")+4);
            //Check for sth like "Host xxx.dtdns.net now points to 127.0.0.1":
            if (response.IndexOfAny("now points to".ToCharArray()) > 0)
            {
                return  new DynamicDNSUpdateResult(DynamicDNSStatus.OK, "");
                 
            }
            else
            {

                switch (response)
                {
                    case "No hostname to update was supplied.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    case "No password was supplied.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    case "The hostname you supplied is not valid.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    case "This account has not yet been activated.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    case "Administration has disabled this account.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    case "Illegal character in IP.":
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                    default:
                        return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, response);
                         
                }
            }
        }

        #endregion
    }
}
